import { Behaviour, getGameObjectById, core, Transform, SpriteRenderer } from "../gameengine";
import { user } from "../user";
import { PickItemBehaviour, ItemBehaviour, HiddenClueBehaviour } from "./item";
import { missionManager } from "../mission/MissionManager";
import { MissionStatus } from "../mission/Mission";
import { itemPrefab } from "../prefab";
import { Rect_Collider } from "../collider/rect_Collider";
import { ConversationTest } from "../conversation/conversationTest";
import { MainRoleBehaviour } from "../mainRoleBehaviour";
import { music } from "../music/music";
import { plot } from "../plot/plot";

export class ItemSystem extends Behaviour {

    private duringTime: number = 0;

    protected onStart() {
        console.log(user.itemRemoveRecord);


    }

    protected onUpdate(advancedTime: number) {

        //根据记录移除物体和pickItemBehaviour
        for (let record of user.itemRemoveRecord) {
            const itemGameObject = getGameObjectById(record.itemID);
            if (!itemGameObject) {    //如果找不到，说明不是该场景物体
                return;
            }
            if (record.isRemoveGameObject == true) {
                itemGameObject.parent.removeChild(itemGameObject);
            } else {
                const pickItemBehaviour = itemGameObject.getScript(PickItemBehaviour);
                itemGameObject.removeScript(pickItemBehaviour);
            }

        }

        //获取到金钥匙
        if (missionManager.missions[1].status == MissionStatus.FINISHED) {
            // const doorwayItem = getGameObjectById("UID写这里");
            // if (doorwayItem) {
            //     //脚印的各种配置写在这里
            //     const footPrintGameObject = core.createPrefab(itemPrefab);
            //     const footPrintTransform = footPrintGameObject.getScript(Transform);
            //     footPrintTransform.x;
            //     footPrintTransform.y;
            //     const footPrintImage = footPrintGameObject.getScript(SpriteRenderer);
            //     footPrintImage.imageName;
            //     const footPrintCollider = new Rect_Collider();
            //     //碰撞盒属性写这里
            //     footPrintGameObject.addScript(footPrintCollider);
            //     const footPrintConversation = new ConversationTest();
            //     //对话属性写这里
            //     footPrintGameObject.addScript(footPrintConversation);
            //     const footPrintPickItemBehaviour = new PickItemBehaviour();
            //     //拾取属性写这里
            //     footPrintGameObject.addScript(footPrintPickItemBehaviour);
            //     doorwayItem.parent.addChild(footPrintGameObject);
            // }
        }

        //获取到隐藏线索12，触发F和鱼缸的变化
        if (missionManager.missions[2].status == MissionStatus.FINISHED) {

            //水池F可拾取+替换图片
            const FGameObject = getGameObjectById("shuichi_F");
            if (FGameObject) {
                const FhiddenClueBehaviour = FGameObject.getScript(HiddenClueBehaviour);
                if (FhiddenClueBehaviour) {
                    FhiddenClueBehaviour.isHiddenClue = false;
                    const FspriteRenderer = FGameObject.getScript(SpriteRenderer);
                    FspriteRenderer.imageName = "assets/item/shuichi_F.png";
                }

                // const Fconversation = FGameObject.getScript(ConversationTest);
            }

            //鱼缸可拾取
            const YGameObject = getGameObjectById("yugang");
            if (YGameObject) {
                const YhiddenClueBehaviour = YGameObject.getScript(HiddenClueBehaviour);
                if (YhiddenClueBehaviour) {
                    YhiddenClueBehaviour.isHiddenClue = false;
                }

                // const YConversationTest = YGameObject.getScript(ConversationTest);
                // c
            }
            //脚印可以看见
            const jiaoyin = getGameObjectById("jiaoyinB2");
            if(jiaoyin){
                const spriteRenderer = jiaoyin.getScript(SpriteRenderer);
                spriteRenderer.imageName = "assets/item/jiaoyinB2.png";
            }
        }

        //获得隐藏线索34，触发棺材变化
        if (missionManager.missions[3].status == MissionStatus.FINISHED) {
            //棺材可拾取
            const GGamObject = getGameObjectById("guancai2");
            if (GGamObject) {
                const GhiddenClueBehaviour = GGamObject.getScript(HiddenClueBehaviour);
                if (GhiddenClueBehaviour) {
                    GhiddenClueBehaviour.isHiddenClue = false;
                }

            }
        }

        //获得隐藏线索5，触发结局2毁灭世界
        if (missionManager.missions[4].status == MissionStatus.FINISHED) {
            //触发结局2毁灭世界
        }

        //正确使用打火机
        if (missionManager.missions[5].status == MissionStatus.FINISHED) {
            // console.log("怪物不敢靠近你了");
            //疯子B需要修改的属性
        }

        //正确使用手枪
        if (missionManager.missions[6].status == MissionStatus.FINISHED) {
            // music.shootPlay();
            // console.log("你杀死了怪物");
            //疯子A需要修改的属性
            const fengziA = getGameObjectById("fengziA");
            if (fengziA) {
                fengziA.parent.removeChild(fengziA);
            }

        }

        //与脚印对话触发结局1变成脚印
        if (missionManager.missions[7].status == MissionStatus.FINISHED) {
            // plot.end1Plot();
            // this.displayObject.parent.removeChild(this.displayObject);
        }

        //使用背包物体的情况
        for (let useBagItem of user.useBagItems) {

            //使用打火机修改蒙版
            if (useBagItem == "fire") {
                const mask = getGameObjectById("mask").getScript(SpriteRenderer);
                if (user.mind <= 40) {
                    mask.imageName = "assets/picture/mask_m_yellow.png";
                }
                if (user.mind == 0) {
                    mask.imageName = "assets/picture/mask_bbb_yellow.png";
                }
                if (user.mind > 40) {
                    mask.imageName = "assets/picture/mask_yellow.png";
                }
            }
            else {

                // if(user.limitedTimeMission == 6){
                //     const mask = getGameObjectById("mask").getScript(SpriteRenderer);
                //     mask.imageName = "assets/picture/mask_black.png"
                // }
            }

        }

        //角色精神值影响蒙版贴图和速度
        if (user.mind <= 60) {
            const mainRoleBehaviour = getGameObjectById("mainRole").getScript(MainRoleBehaviour);
            mainRoleBehaviour.SPEED = 4;

        }
        if (user.mind <= 40) {
            const mask = getGameObjectById("mask").getScript(SpriteRenderer);
            mask.imageName = "assets/picture/mask_m.png"
        }
        if (user.mind == 0) {
            const mask = getGameObjectById("mask").getScript(SpriteRenderer);
            mask.imageName = "assets/picture/mask_bbb.png"
        }
        // if (user.limitedTimeMission == 6) {
        //     const mask = getGameObjectById("mask").getScript(SpriteRenderer);
        //     mask.imageName = "assets/picture/mask_black.png"
        // }


        // console.log(this.duringTime);
    }


}